package com.jboost.modules.app.service.strategry.gc;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.GCOrderReq;
import com.jboost.modules.app.dto.GcKcDto;
import com.jboost.modules.app.dto.HsMatrierDto;
import com.jboost.modules.app.dto.resp.GcKcResp;
import com.jboost.modules.app.dto.resp.GcOrderResp;
import com.jboost.modules.app.dto.resp.GcProductResp;
import com.jboost.modules.app.dto.resp.GcWlResp;
import com.jboost.modules.system.entity.HsMaterialNeed;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.jboost.modules.app.service.strategry.gc.HsGcKcQuery.PRODUCT_INFO;
import com.jboost.modules.app.constant.*;
@Service(value = "HsGcOrderQuery")
@Slf4j
public class HsGcOrderQuery extends AbstractGcService<JsonObject, Boolean> {

    private final static String ORDER_API = "https://oms.goodcang.net/public_open/order/get_order_list";
    private final static String ORDER_API_TEST = url_prefix_test + "/order/get_order_list";


    public String getFormId() {
        return "64704bb763b7d2000849daf7";
    }

    @Async
    @Scheduled(cron = "0 0/50 * * * ?")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        log.info("开始批量查询gc订单数据");


        List<GcOrderResp.DataBean> list = list(ORDER_API_TEST, gcHeaders, GcOrderResp.class);

        for (GcOrderResp.DataBean e:list){
            if(!pass(e.getOrder_code(),e)){
                continue;
            }
            GCOrderReq.GcOrderInsert.GcOrderInsertBuilder gcOrderInsertBuilder = GCOrderReq.GcOrderInsert.builder().order_code(getParaMap(e.getOrder_code()))
                    .reference_no(getParaMap(e.getReference_no()))
                    .platform(getParaMap(e.getPlatform()))
                    .order_status(getParaMap(order_status_enum.D.getDescByType(e.getOrder_status())))
                    .shipping_method(getParaMap(e.getShipping_method()))
                    .fba_shipment_id(getParaMap(e.getFba_shipment_id()))
                    .fba_shipment_id_create_time(getParaMap(e.getFba_shipment_id_create_time()))
                    .tracking_no(getParaMap(e.getTracking_no()))
                    .warehouse_code(getParaMap(e.getWarehouse_code()))
                    .wp_code(getParaMap(e.getWp_code()))
                    .order_weight(getParaMap(e.getOrder_weight()))
                    .order_desc(getParaMap(e.getOrder_desc()))
                    .date_create(getParaMap(e.getDate_create()))
                    .date_shipping(getParaMap(e.getDate_shipping()))
                    .date_modify(getParaMap(e.getDate_modify()))
                    .abnormal_problem_reason(getParaMap(e.getAbnormal_problem_reason()))
                    .estimated_arrival_date(getParaMap(e.getEstimated_arrival_date()))
                    .estimated_arrival_time(getParaMap(e.getEstimated_arrival_time()))
                    .consignee_country_code(getParaMap(e.getConsignee_country_code()))
                    .consignee_country_name(getParaMap(e.getConsignee_country_name()))
                    .consignee_state(getParaMap(e.getConsignee_state()))
                    .consignee_city(getParaMap(e.getConsignee_city()))
                    .consignee_district(getParaMap(e.getConsignee_district()))
                    .consignee_address1(getParaMap(e.getConsignee_address1()))
                    .consignee_address2(getParaMap(e.getConsignee_address2()))
                    .consignee_address3(getParaMap(e.getConsignee_address3()))
                    .consignee_zipcode(getParaMap(e.getConsignee_zipcode()))
                    .consignee_doorplate(getParaMap(e.getConsignee_doorplate()))
                    .consignee_company(getParaMap(e.getConsignee_company()))
                    .consignee_name(getParaMap(e.getConsignee_name()))
                    .consignee_last_name(getParaMap(e.getConsignee_last_name()))
                    .consignee_phone(getParaMap(e.getConsignee_phone()))
                    .consignee_email(getParaMap(e.getConsignee_email()))
                    .age_detection(getParaMap(age_detection_enum.C.getDescByType(String.valueOf(e.getAge_detection()))))
                    .lift_gate(getParaMap(LiftGate_enum.A.getDescByType(String.valueOf(e.getLiftGate()))))
                    .total_fee(getParaMap(e.getFee_details().getTotalFee()))
                    .shipping(getParaMap(e.getFee_details().getSHIPPING()))
                    .opf(getParaMap(e.getFee_details().getOPF()))
                    .fsc(getParaMap(e.getFee_details().getFSC()))
                    .dt(getParaMap(e.getFee_details().getDT()))
                    .otf(getParaMap(e.getFee_details().getOTF()))
                    .currency_code(getParaMap(e.getFee_details().getCurrency_code()))
                    .s_name(getParaMap(e.getSender_info().getName()))
                    .s_phone(getParaMap(e.getSender_info().getPhone()))
                    .v_ioss_number(getParaMap(e.getVat_change_info().getIoss_number()))
                    .v_pid_number(getParaMap(e.getVat_change_info().getPid_number()))
                    .v_recipient_eori(getParaMap(e.getVat_change_info().getRecipient_eori()))
                    .v_shipper_eori(getParaMap(e.getVat_change_info().getShipper_eori()))
                    .v_shipper_vat(getParaMap(e.getVat_change_info().getShipper_vat()))
                    .v_recipient_vat_country(getParaMap(e.getVat_change_info().getRecipient_vat_country()))
                    .v_recipient_eori_country(getParaMap(e.getVat_change_info().getRecipient_eori_country()))
                    .v_shipper_company_name(getParaMap(e.getVat_change_info().getShipper_company_name()))
                    .payment_time(getParaMap(e.getPayment_time()))
                    .is_warehouse_packing(getParaMap(is_warehouse_packing_enum.A.getDescByType(e.getIs_warehouse_packing() + "")))
                    .t_reference_id(getParaMap(e.getTruck_info().getReference_id()))
                    .t_seller_name(getParaMap(e.getTruck_info().getSeller_name()))
                    .t_fba_warehouse_code(getParaMap(e.getTruck_info().getFba_warehouse_code()))
                    .property_label(getParaMap(e.getProperty_label()))
                    .customer_package_requirement(getParaMap(customer_package_enum.HBD.getDescByType(e.getCustomer_package_requirement() + "")))
                    .vas_logistics_recommendation_option(getParaMap(logistics_recommendation_enum.B.getDescByType(e.getVas().getLogistics_recommendation_option() + "")));
            List<GCOrderReq.ItemsBean> itemsBeans = new ArrayList<>();
            List<GcOrderResp.DataBean.ItemsBean> items1 = e.getItems();
            items1.forEach(e1->{
                GCOrderReq.ItemsBean build = GCOrderReq.ItemsBean.builder().item_id(getParaMap(e1.getItem_id()))
                        .hs_code(getParaMap(e1.getHs_code()))
                        .fba_product_code(getParaMap(e1.getFba_product_code()))
                        .product_declared_value(getParaMap(e1.getProduct_declared_value()))
                        .product_sku(getParaMap(e1.getProduct_sku()))
                        .label_replacement_qty(getParaMap(e1.getLabel_replacement_qty()))
                        .quantity(getParaMap(e1.getQuantity()))
                        .transaction_id(getParaMap(e1.getTransaction_id())).build();
                itemsBeans.add(build);
            });
            gcOrderInsertBuilder.items(getParaMap(itemsBeans));
            List<GCOrderReq.OrderBoxInfoBean> orderBoxInfoBeans = new ArrayList<>();
            List<GcOrderResp.DataBean.OrderBoxInfoBean> orderBoxInfo = e.getOrderBoxInfo();
            orderBoxInfo.forEach(e2->{
                GCOrderReq.OrderBoxInfoBean.OrderBoxInfoBeanBuilder orderBoxInfoBeanBuilder = GCOrderReq.OrderBoxInfoBean.builder().box_no(getParaMap(e2.getBox_no()))
                        .border_code(getParaMap(e2.getOrder_code()))
                        .box_mark(getParaMap(e2.getBox_mark()))
                        .ob_add_time(getParaMap(e2.getOb_add_time()))
                        .fba_box_mark(getParaMap(e2.getFba_box_mark()))
                        .ob_qty(getParaMap(e2.getOb_qty()))
                        .ob_length(getParaMap(e2.getOb_length()))
                        .ob_weight(getParaMap(e2.getOb_weight()))
                        .ob_width(getParaMap(e2.getOb_width()))
                        .product_barcode(getParaMap(e2.getProduct_barcode()))
                        .ob_height(getParaMap(e2.getOb_height()))
                        .tracking_number(getParaMap(e2.getTracking_number()));
                GCOrderReq.OrderBoxInfoBean build = orderBoxInfoBeanBuilder.build();
                orderBoxInfoBeans.add(build);
            });

            gcOrderInsertBuilder.order_box_info(getParaMap(orderBoxInfo));
            GCOrderReq.GcOrderInsert gcOrderInsert = gcOrderInsertBuilder.build();
            String s = null;
            try {
                s = jdyCreate(gcOrderInsert);
                HashMap map = JSONUtil.fromJson(s, HashMap.class);
                String dataId = ((JSONObject) map.get("data")).getString("_id");
                save(e.getOrder_code(),JSONUtil.toJson(e),dataId);
            } catch (IOException ex) {
                log.info("调用简道云接口失败");
            }
            log.info("开始批量查询gc订单数据结束" + s);
        }
        return BaseResult.success();
    }



}
